import Vue from 'vue'
import VueRouter from 'vue-router'
import { sync } from 'vuex-router-sync'

import Home from './pages/home/home.vue'
import Cart from './pages/cart/cart.vue'
import ProductList from './pages/productList/productList.vue'
import Pay from './pages/pay/pay.vue'
import OrderList from './pages/order/orderList.vue'
import OrderInfo from './pages/orderInfo/orderInfo.vue'
Vue.use(VueRouter)
import App from './App'
// import App from './pages/index/index.vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import vuexI18n from 'vuex-i18n'

require('es6-promise').polyfill()

/**
* you can add your module here
*/
let store = new Vuex.Store({
  modules: {
    i18n: vuexI18n.store
  }
})

store.registerModule('vux', {
  state: {
    demoScrollTop: 0,
    isLoading: false,
    direction: 'forward',
    hotel: 'no',
    userId:'no',
    openid:"no"
  },
  mutations: {
    updateDemoPosition (state, payload) {
      state.demoScrollTop = payload.top
    },
    updateLoadingStatus (state, payload) {
      state.isLoading = payload.isLoading
    },
    updateDirection (state, payload) {
      state.direction = payload.direction
    },
    updateHotel (state, hotel) {
      state.hotel = hotel
    },
    updateuserId (state, userId) {
      state.userId = userId.userId
    },
    updateopenId (state, openid) {
      state.openid = openid.openid
    }
  },
  actions: {
    updateLoadingStatus({commit},isLoading){
      commit({type: 'updateLoadingStatus', isLoading: isLoading})
    },
    updateDemoPosition ({commit}, top) {
      commit({type: 'updateDemoPosition', top: top})
    },
    updateHotel ({commit}, hotel) {
       commit({type: 'updateHotel', hotel: hotel})
    },
    updateuserId ({commit}, userId) {
       commit({type: 'updateuserId', userId:userId})
    },
    updateopenId ({commit}, openid) {
       commit({type: 'updateopenId', openid:openid})
    }
  },
  getters:{
    getHotel: state => {  
        return state.hotel;  
    },
    getUserId:state => {
      return state.userId
    },
    getLoadingState:state => {
      return state.isLoading
    },
    getOpenid:state => {
      return state.openid
    }
  }
})

Vue.use(vuexI18n.plugin, store)

// plugins
import { LocalePlugin, DevicePlugin, ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin, WechatPlugin, AjaxPlugin, AppPlugin } from 'vux'
Vue.use(DevicePlugin)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(ConfirmPlugin)
Vue.use(LoadingPlugin)
Vue.use(WechatPlugin)
Vue.use(AjaxPlugin)
Vue.use(LocalePlugin)

// test
if (process.env.platform === 'app') {
  Vue.use(AppPlugin, store)
}

const wx = Vue.wechat
const http = Vue.http

/**
* -------------------------- 微信分享 ----------------------
* 请不要直接复制下面代码
*/

wx.ready(() => {
  console.log('wechat ready')
  wx.onMenuShareAppMessage({
    title: 'VUX', // 分享标题
    desc: '基于 WeUI 和 Vue 的移动端 UI 组件库',
    link: 'https://vux.li?x-page=wechat_share_message',
    imgUrl: 'https://static.vux.li/logo_520.png'
  })

  wx.onMenuShareTimeline({
    title: 'VUX', // 分享标题
    desc: '基于 WeUI 和 Vue 的移动端 UI 组件库',
    link: 'https://vux.li?x-page=wechat_share_timeline',
    imgUrl: 'https://static.vux.li/logo_520.png'
  })
})

const permissions = JSON.stringify(['onMenuShareTimeline', 'onMenuShareAppMessage'])
const url = document.location.href
http.post('https://vux.li/jssdk?url=' + encodeURIComponent(url.split('#')[0]) + '&jsApiList=' + permissions).then(res => {
  wx.config(res.data.data)
})

import objectAssign from 'object-assign'

const vuxLocales = require('json-loader!yaml-loader!vux/src/locales/all.yml')
const componentsLocales = require('json-loader!yaml-loader!src/locales/components.yml')
const globalLocales = require('json-loader!yaml-loader!src/locales/global_locales.yml') || {en: {}, 'zh-CN': {}}

const finalLocales = {
  'en': objectAssign(vuxLocales['en'], componentsLocales['en'], globalLocales['en']),
  'zh-CN': objectAssign(vuxLocales['zh-CN'], componentsLocales['zh-CN'], globalLocales['zh-CN'])
}

for (let i in finalLocales) {
  Vue.i18n.add(i, finalLocales[i])
}

const nowLocale = Vue.locale.get()
if (/zh/.test(nowLocale)) {
  Vue.i18n.set('zh-CN')
} else {
  Vue.i18n.set('en')
}

const FastClick = require('fastclick')
FastClick.attach(document.body)

// The following line will be replaced with by vux-loader with routes in ./demo_list.json
const routes = [
  { path: '/', component: Home, name: 'Home' },
  { path: '/cart', component: Cart, name: 'cart' },
  { path: '/ProductList', component: ProductList, name: 'ProductList' },
  { path: '/pay', component: Pay, name: 'pay' },
  { path: '/orderList', component: OrderList, name: 'orderList'},
  { path: '/orderInfo/:id', component: OrderInfo, name: 'orderInfo'}
]

const router = new VueRouter({
  routes
})

sync(store, router)

// simple history management
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

router.beforeEach(function (to, from, next) {
  store.commit('updateLoadingStatus', {isLoading: true})

  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)

  if (toIndex) {
    if (toIndex > fromIndex || !fromIndex || (toIndex === '0' && fromIndex === '0')) {
      store.commit('updateDirection', {direction: 'forward'})
    } else {
      store.commit('updateDirection', {direction: 'reverse'})
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    store.commit('updateDirection', {direction: 'forward'})
  }

  if (/\/http/.test(to.path)) {
    let url = to.path.split('http')[1]
    window.location.href = `http${url}`
  } else {
    next()
  }
})

router.afterEach(function (to) {
  store.commit('updateLoadingStatus', {isLoading: false})
  ga && ga('set', 'page', to.fullPath)
  ga && ga('send', 'pageview')
})

new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
